﻿/*
  模块名称：数据授权
  模块功能：对菜单角色的行和列进行授权，二次开发人员，请理解代码后，再进行修改；
  当前版本：1.0
  开发人员：马立涛
  开始时间：2005-10-18
  
  修改时间：2008-10-10
  修 改 人：马立涛
 
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using Baimasoft.Manage.SqlServerDAL;

public partial class SysManage_DataRole_Edit : System.Web.UI.Page
{
    DbRoleManage MyDBRM = new DbRoleManage();
    RoleManage MyRm = new RoleManage();
    public string sOrgtype = "", m_sFuncRoleCode = "", sHcode = "", sDbRole = "";//对应功能操作角色代码
    public string sHtml = "", ll = "", sEdit = "";
    public static string sDataRowHtml = "", sDataFieldHtml = "";
    public string sEditType = "1";

    protected void Page_Load(object sender, EventArgs e)
    {
        HOrgIDCol.Value = AspNetHelper.GetRightOrgIds();   //初始化树的数据权限

        try
        {
            sOrgtype = Request.QueryString["sOrgtype"].ToString().Trim();
        }
        catch
        {
            sOrgtype = "";
        }

        try
        {
            sDbRole = Request.QueryString["DbRole"].ToString().Trim();  //数据角色ID
        }
        catch
        {
            sDbRole = "";
        }


        try
        {
            m_sFuncRoleCode = Request.QueryString["CtrlRole"].ToString().Trim();  //功能角色ID
        }
        catch
        {
            m_sFuncRoleCode = "";
        }

        try
        {
            m_sFuncRoleCode = Request.QueryString["CtrlRole"].ToString().Trim();
        }
        catch
        {
            m_sFuncRoleCode = "";
        }

        try
        {
            sHcode = Request.QueryString["Hcode"].ToString().Trim();
        }
        catch
        {
            sHcode = "";
        }

        try
        {
            sEditType = Request.QueryString["EditType"].ToString().Trim();
        }
        catch
        {
            sEditType = "1";
        }

        if (ViewState["VHtml"] == null)
        {
            ViewState["VHtml"] = "";
        }
        sHtml = ViewState["VHtml"].ToString().Trim();

        if (!IsPostBack)
        {

            if (sEditType == "1")
            {
                Role_Code.Text = MyRm .GetMaxNumber ("Sys_DA_Role", "Role_Code");
            }

            DataRowCtrl.Checked = true;
            DataRowView.Style.Add("display", "");
            //DataFieldView.Style.Add("display", "none");
            sDataRowHtml = "";
            sDataFieldHtml = "";
            CtrlRoleBind();//功能操作角色绑定             

            if (sEditType.Trim() == "2")
            {
            }

            DataFieldBind();//字段展现
            if (m_sFuncRoleCode.Trim() != "")
                MenuView();//显示菜单
        }
    }

    /// <summary>
    /// 功能操作角色绑定
    /// </summary>
    public void CtrlRoleBind()
    {
        Ctrl_Role.DataSource = MyRm .GetMyCtrlRole((string)Session["Baimasoft_Role_Code"], "1", "");
        Ctrl_Role.DataBind();
        Ctrl_Role.Items.Insert(0, "全部角色");
        Ctrl_Role.Items[0].Value = "0";
    }

    /// <summary>
    /// 菜单角色级联关系
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Ctrl_Role_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DataRowCtrl.Checked == true)
        {
            DataRowCtrl.Checked = true;
            DataRowView.Style.Add("display", "");
        }
        else
        {
            FieldCtrl.Checked = true;
            DataFieldView.Style.Add("display", "");
        }
        //DataFieldView.Style.Add("display", "none");

        string sTreeHtml = "";
        string sRootLeaflessIconHtml = "<img height=\"20\" src=\"img/sys/root_leafless.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sRootSpreadIconHtml = "<img height=\"20\" src=\"img/sys/root_spread.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sMidLeaflessIconHtml = "<img height=\"20\" src=\"img/sys/mid_leafless.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sMidSpreadIconHtml = "<img height=\"20\" src=\"img/sys/mid_spread.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sRearLeaflessIconHtml = "<img height=\"20\" src=\"img/sys/rear_leafless.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sRearSpreadIconHtml = "<img height=\"20\" src=\"img/sys/rear_spread.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sFolderCloseIconHtml = "<img height=\"20\" src=\"img/sys/folder_close.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sFolderOpenIconHtml = "<img height=\"20\" src=\"img/sys/folder_open.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sPageUnselectedIconHtml = "<img height=\"20\" src=\"img/sys/page_unselected.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sVerticalIconHtml = "<img height=\"20\" src=\"img/sys/vertical.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sBlankIconHtml = "<img src=\"img/sys/blank.gif\" align=\"absmiddle\" border=\"0\" WIDTH=\"19\" HEIGHT=\"20\">";

        string sUserRoleCode = Ctrl_Role.SelectedValue.ToString().Trim(); // Session["Baimasoft_Role_Code"].ToString().Trim();

        string sPageIDCol = "\"";
        string sPageDocIDCol = "\""; //文档ID集合
        SqlDataReader ShowGrantTreeReader2 = MyDBRM .GetMyMenuTree(sUserRoleCode);
        int nPageCount = 0; //是否有被授权使用的页面

        while (ShowGrantTreeReader2.Read())
        {
            //从数据库存储过程所创建的临时表中取数据
            string sIndentVertical = ShowGrantTreeReader2["IndentVertical"].ToString().Trim();   //展开结点树时，缩进的结点前面是否有上(几)级结点的垂线
            string isLastSibling   = ShowGrantTreeReader2["LastSibling"].ToString().Trim();     //是否同族的最后一个结点
            string isLeafed        = ShowGrantTreeReader2["Leafed"].ToString().Trim();          //是否有叶子结点
            string sCode           = ShowGrantTreeReader2["Menu_Code"].ToString().Trim();       // 结点编码
            string cType           = ShowGrantTreeReader2["Type"].ToString().Trim();            //结点类型
            int iHomologID         = (int)ShowGrantTreeReader2["Page_ID"];                       //若是页面其对应的页面ID
            string sLabel          = ShowGrantTreeReader2["Label"].ToString().Trim();           //结点标签
            string sIFDbGrant      = ShowGrantTreeReader2["IFDbGrant"].ToString().Trim();       //是否可进行组织部门选择

            //组合html语句
            string sLabelHtml = "&nbsp;" + sLabel;
            if (sCode.Equals("Men"))
            { //根结点
                string sRootHtml = "";
                if (isLeafed.Equals("0"))
                { //无子结点
                    sRootHtml = sRootLeaflessIconHtml + sFolderCloseIconHtml + sLabelHtml;
                    break;
                }
                else
                    sRootHtml = sRootSpreadIconHtml + sFolderOpenIconHtml + sLabelHtml;
                sTreeHtml = sTreeHtml + "<div>" + sRootHtml + "</div>";
            }
            else
            { //非根结点
                string sNodeHtml = "";
                // 处理缩进 
                string sIndentHtml = "";
                int i = 0;
                char cIsVertical;
                while (i < sIndentVertical.Length)
                {
                    cIsVertical = sIndentVertical[i];
                    if (cIsVertical == '0')
                        sIndentHtml = sIndentHtml + sBlankIconHtml;
                    else
                        sIndentHtml = sIndentHtml + sVerticalIconHtml;
                    i = i + 1;
                }

                // 结点 
                if (cType.Equals("1"))
                { //1.树枝结点
                    if (isLastSibling.Equals("0"))
                    { //2.非尾结点
                        if (isLeafed.Equals("1")) //3.有子结点
                            sNodeHtml = sMidSpreadIconHtml + sFolderOpenIconHtml + sLabelHtml;
                        else //3.无子结点
                            sNodeHtml = sMidLeaflessIconHtml + sFolderCloseIconHtml + sLabelHtml;
                    }
                    else
                    { //2.尾结点

                        if (isLeafed.Equals("1")) //3.有子结点
                            sNodeHtml = sRearSpreadIconHtml + sFolderOpenIconHtml + sLabelHtml;
                        else //3.无子结点
                            sNodeHtml = sRearLeaflessIconHtml + sFolderCloseIconHtml + sLabelHtml;
                    }
                }
                else if (cType.Equals("0"))
                { //1.树叶结点
                    nPageCount++;
                    //BtnInfo = UMConn.MyBtnInfo(sUserRoleCode, iHomologID);
                    //string sPageChkboxHtml = "<input type=\"checkbox\" name=\"chkPage\" value=\"" + iHomologID + "\" onclick=\"pageChkboxClick(this)\"/>&nbsp;&nbsp;" + BtnInfo;
                    string sOrgPersChoice = "";
                    string sRoleChoice = "";
                    string sDocChoice = "";
                    if (sIFDbGrant.Trim() == "1")   //可以进行数据角色授权
                    {
                        if (iHomologID == 300 || iHomologID == 301 || iHomologID == 302 || iHomologID == 303 || iHomologID == 304)
                        {
                            //文件和文件夹选择
                            sDocChoice = "&nbsp;<input type=\"text\" size=\"20\" id=\"ChoiceDocName_" + iHomologID.ToString().Trim() + "\" onclick=\"GetMyFolderFileTree(" + iHomologID.ToString().Trim() + ")\" runat=\"server\" />";
                            sDocChoice = sDocChoice + " <input type=\"hidden\"  size=\"20\" id=\"HChoiceDocID_" + iHomologID.ToString().Trim() + "\" runat=\"server\" /> ";
                            sDocChoice = sDocChoice + "<input id=\"GrantBtn1\" type=\"button\"  onclick=\"GetMyFolderFileTree(" + iHomologID.ToString().Trim() + ")\" value =\"文档\" /> ";

                            sPageDocIDCol = sPageDocIDCol + iHomologID.ToString().Trim() + ",";   //可以数据角色授权的文档页面ID集合
                        }
                        else
                        {
                            //行政组织树和人员选择
                            sOrgPersChoice = "&nbsp;<input type=\"text\" size=\"20\" id=\"ChoiceName_" + iHomologID.ToString().Trim() + "\" onclick=\"GetMyOrgTreeClick(" + iHomologID.ToString().Trim() + ")\" runat=\"server\" />";
                            sOrgPersChoice = sOrgPersChoice + " <input type=\"hidden\"  size=\"20\" id=\"HChoiceID_" + iHomologID.ToString().Trim() + "\" runat=\"server\" /> ";
                            sOrgPersChoice = sOrgPersChoice + "<input id=\"GrantBtn1\" type=\"button\"  onclick=\"GetMyOrgTreeClick(" + iHomologID.ToString().Trim() + ")\" value =\"组织\" /> ";

                            //角色选择
                            sRoleChoice = "&nbsp;<input type=\"text\" size=\"20\" id=\"ChoiceRoleName_" + iHomologID.ToString().Trim() + "\" onclick=\"GetMyRoleTree(" + iHomologID.ToString().Trim() + ")\" runat=\"server\" />";
                            sRoleChoice = sRoleChoice + " <input type=\"hidden\"  size=\"20\" id=\"HChoiceRoleID_" + iHomologID.ToString().Trim() + "\" runat=\"server\" /> ";
                            sRoleChoice = sRoleChoice + "<input id=\"GrantBtn1\" type=\"button\"  onclick=\"GetMyRoleTree(" + iHomologID.ToString().Trim() + ")\" value =\"角色\" /> ";

                            sPageIDCol = sPageIDCol + iHomologID.ToString().Trim() + ",";   //可以数据角色授权的页面ID集合
                        }
                    }

                    if (isLastSibling.Equals("0")) //2.非尾结点
                        //sNodeHtml = sMidLeaflessIconHtml + sPageUnselectedIconHtml +sLabelHtml + sPageChkboxHtml + sBtnHtml;
                        sNodeHtml = sMidLeaflessIconHtml + sPageUnselectedIconHtml + sLabelHtml + sOrgPersChoice + sRoleChoice + sDocChoice;
                    else //2.尾结点

                        //sNodeHtml = sRearLeaflessIconHtml + sPageUnselectedIconHtml +sLabelHtml + sPageChkboxHtml + sBtnHtml;
                        sNodeHtml = sRearLeaflessIconHtml + sPageUnselectedIconHtml + sLabelHtml + sOrgPersChoice + sRoleChoice + sDocChoice;
                }
                sNodeHtml = sIndentHtml + sNodeHtml;
                sTreeHtml = sTreeHtml + "<div>" + sNodeHtml + "</div>";
            }
        }
        if (ShowGrantTreeReader2 != null)
            ShowGrantTreeReader2.Close();      

        sPageIDCol = sPageIDCol + "\"";
        sPageDocIDCol = sPageDocIDCol + "\"";

        //选择组织树

        string sJS10 = "var sOrgIDCol = form1.HOrgIDCol.value;var sFeatures = \"dialogHeight:600px;dialogWidth:700px;resizeable:no;help:no;status:no\"; var obj = showModalDialog(\"ChoiceOrgPersInfo.aspx?ChoiceID=\"+obj+\"&OrgType=" + sOrgtype + "&OrgIDCol=\" + sOrgIDCol,window,sFeatures); if(obj != undefined){ sChoiceID = obj.sChoiceID;ChoiceNameInfo = obj.ChoiceNameInfo;ChoiceIDInfo=obj.ChoiceIDInfo; eval(\"form1.ChoiceName_\"+sChoiceID+\".value=\"+ChoiceNameInfo+\";\"); eval(\"form1.HChoiceID_\"+sChoiceID+\".value=\" + ChoiceIDInfo + \";\");eval(\"form1.ChoiceName_\"+sChoiceID+\".blur();\"); } ";
        string sPageChkboxClickJS = "var sChoiceID;var ChoiceNameInfo;var ChoiceIDInfo;var iChoicePoNumber =0; function GetMyOrgTreeClick(obj){ " + sJS10 + "}";

        //选择角色树

        string sJS11 = "var sFeatures = \"dialogHeight:600px;dialogWidth:400px;resizeable:no;help:no;status:no\"; var obj = showModalDialog(\"ChoiceRoleTree.aspx?PageID=\"+sPageID+\"&OrgType=" + sOrgtype + "\",window,sFeatures); if(obj != undefined){ sPageRoleInfo = obj.sPageRoleInfo;ChoiceRoleNameInfo = obj.ChoiceRoleNameInfo;ChoiceRoleIDInfo=obj.ChoiceRoleIDInfo; eval(\"form1.ChoiceRoleName_\"+sPageRoleInfo+\".value=\"+ChoiceRoleNameInfo+\";\"); eval(\"form1.HChoiceRoleID_\"+sPageRoleInfo+\".value=\"+ChoiceRoleIDInfo+\";\"); eval(\"form1.ChoiceRoleName_\"+sPageRoleInfo+\".blur();\"); } ";
        string sPageRoleClickJS = "var sPageRoleInfo;var ChoiceRoleNameInfo;var ChoiceRoleIDInfo;var iChoiceRolePoNumber =0; function GetMyRoleTree(obj){ var sPageID=obj; " + sJS11 + "}";

        //选择文档树
        string sJS12 = "var sOrgIDCol = form1.HOrgIDCol.value;var sFeatures = \"dialogHeight:600px;dialogWidth:700px;resizeable:no;help:no;status:no\"; var obj = showModalDialog(\"../common/ChoiceFolderFiles_Index.aspx?ChoiceID=\"+obj,window,sFeatures);if(obj != undefined){ sChoiceFolderFileID = obj.sChoiceFolderFileID;ChoiceFolderFileNameInfo = obj.ChoiceFolderFileNameInfo;ChoiceFolderFileIDInfo=obj.ChoiceFolderFileIDInfo; eval(\"form1.ChoiceDocName_\"+sChoiceFolderFileID+\".value=\"+ChoiceFolderFileNameInfo+\";\"); eval(\"form1.HChoiceDocID_\"+sChoiceFolderFileID+\".value=\" + ChoiceFolderFileIDInfo + \";\"); eval(\"form1.ChoiceDocName_\"+sChoiceFolderFileID+\".blur();\"); }";
        string sPageDocClickJS = "var sChoiceFolderFileID;var ChoiceFolderFileNameInfo;var ChoiceFolderFileIDInfo;var iChoiceDocNumber =0; function GetMyFolderFileTree(obj){ " + sJS12 + "}";

        string sJS = "<script>" + sPageChkboxClickJS + sPageRoleClickJS + sPageDocClickJS + "form1.PageIDCol.value=" + sPageIDCol + ";form1.PageDocIDCol.value=" + sPageDocIDCol + ";</script>";
        sHtml = sTreeHtml + sJS;
        ViewState["VHtml"] = sHtml;
    }

    /// <summary>
    /// 数据字段绑定
    /// </summary>
    public void DataFieldBind()
    {
        sDataFieldHtml = "";
        string TableID = "";
        string LastSign = "";//上一次标记


        DataTable MyTb = new DataTable();
        MyTb = MyDBRM.GetFieldInfo();//查询所有字段信息


        sDataFieldHtml = " <table cellspacing=\"0\" cellpadding=\"0\" >";
        for (int i = 0; i < MyTb.Rows .Count ; i++)
        {

            if (MyTb.Rows [i][2].ToString().Trim() == "1")//表信息
            {
                sDataFieldHtml += "<tr><td><table cellspacing=\"0\" cellpadding=\"0\">";
                sDataFieldHtml += "<td><img id=\"";
                sDataFieldHtml += "Tab" + MyTb.Rows[i][0].ToString().Trim() + "img";
                sDataFieldHtml += "\" src=\"image/jian.gif\" onclick=\"javascript:Fieldshowsub('";
                sDataFieldHtml += "Tab" + MyTb.Rows[i][0].ToString().Trim();
                sDataFieldHtml += "')\">";
                sDataFieldHtml += "</td><td ><img src=\"image/top.gif\" /></td>";
                sDataFieldHtml += "<td>" + MyTb.Rows[i][1].ToString().Trim() + "</td></tr></td></table></tr>";
                TableID = "Tab" + MyTb.Rows[i][0].ToString().Trim();
                LastSign = MyTb.Rows[i][2].ToString().Trim();
            }
            else//字段信息
            {
                if (LastSign.Trim() == "1")
                    sDataFieldHtml += "<tr id=\"" + TableID.Trim() + "\" style=\"display:block\"><td><table   cellspacing=\"0\" cellpadding=\"0\"><tr>";
                else
                    sDataFieldHtml += "<tr>";
                sDataFieldHtml += "<td width=\"20\"><img src=\"image/line1.gif\" /></td><td><img id=\"";
                sDataFieldHtml += "img" + MyTb.Rows[i][0].ToString().Trim();
                if (MyTb.Rows[i][3].ToString().Trim() == "1")//表中最后一个字段
                    sDataFieldHtml += "\" src=\"image/line3.gif\"/></td><td><img src=\"image/page.gif\" /></td>";
                else
                    sDataFieldHtml += "\" src=\"image/line2.gif\"/></td><td><img src=\"image/page.gif\" /></td>";
                sDataFieldHtml += "<td>";
                sDataFieldHtml += MyTb.Rows[i][1].ToString().Trim() + "</td>";

                sDataFieldHtml += "<TD> <input type=\"checkbox\" id=\"" + MyTb.Rows[i][0].ToString().Trim() + ".W\" onclick=\"FieldCheck(this)\"/>可读写 " +
                                  " <input type=\"checkbox\" id=\"" + MyTb.Rows[i][0].ToString().Trim() + ".R\" onclick=\"FieldCheck(this)\"/>只读 " +
                       "<input type=\"checkbox\" id=\"" + MyTb.Rows[i][0].ToString().Trim() + ".H\" onclick=\"FieldCheck(this)\"/>隐藏</td> ";

                //sDataFieldHtml += "<input type=\"checkbox\" id=\"Field" + MyDv[i][0].ToString().Trim() + "\" onclick=\"FieldCheck(this)\"/>";
                sDataFieldHtml += "</td></tr>";
                if (MyTb.Rows[i][3].ToString().Trim() == "1")//一个表中最后一个字段


                    sDataFieldHtml += "</table></td></tr>";
                LastSign = MyTb.Rows[i][2].ToString().Trim();
            }
        }
        sDataFieldHtml += "</table>";

    }

    /// <summary>
    /// 菜单展示
    /// </summary>
    public void MenuView()
    {
        if (DataRowCtrl.Checked == true)
        {
            DataRowCtrl.Checked = true;
            DataRowView.Style.Add("display", "");
        }
        else
        {
            FieldCtrl.Checked = true;
            DataFieldView.Style.Add("display", "");
        }
        //DataFieldView.Style.Add("display", "none");

        string sTreeHtml = "";
        string sRootLeaflessIconHtml = "<img height=\"20\" src=\"img/sys/root_leafless.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sRootSpreadIconHtml = "<img height=\"20\" src=\"img/sys/root_spread.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sMidLeaflessIconHtml = "<img height=\"20\" src=\"img/sys/mid_leafless.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sMidSpreadIconHtml = "<img height=\"20\" src=\"img/sys/mid_spread.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sRearLeaflessIconHtml = "<img height=\"20\" src=\"img/sys/rear_leafless.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sRearSpreadIconHtml = "<img height=\"20\" src=\"img/sys/rear_spread.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sFolderCloseIconHtml = "<img height=\"20\" src=\"img/sys/folder_close.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sFolderOpenIconHtml = "<img height=\"20\" src=\"img/sys/folder_open.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sPageUnselectedIconHtml = "<img height=\"20\" src=\"img/sys/page_unselected.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sVerticalIconHtml = "<img height=\"20\" src=\"img/sys/vertical.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        //string sButtonIconHtml = "<img height=\"20\" src=\"img/sys/button.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sBlankIconHtml = "<img src=\"img/sys/blank.gif\" align=\"absmiddle\" border=\"0\" WIDTH=\"19\" HEIGHT=\"20\">";

        string sUserRoleCode = m_sFuncRoleCode; // Session["Baimasoft_Role_Code"].ToString().Trim();

        string sPageIDCol = "\"";
        string sPageDocIDCol = "\"";
        SqlDataReader ShowGrantTreeReader2 = MyDBRM.GetMyMenuTree(sUserRoleCode);
        int nPageCount = 0; //是否有被授权使用的页面

        while (ShowGrantTreeReader2.Read())
        {
            //从数据库存储过程所创建的临时表中取数据
            string sIndentVertical = ShowGrantTreeReader2["IndentVertical"].ToString().Trim();   //展开结点树时，缩进的结点前面是否有上(几)级结点的垂线
            string isLastSibling = ShowGrantTreeReader2["LastSibling"].ToString().Trim();     //是否同族的最后一个结点

            string isLeafed = ShowGrantTreeReader2["Leafed"].ToString().Trim();          //是否有叶子结点

            string sCode = ShowGrantTreeReader2["Menu_Code"].ToString().Trim();       // 结点编码
            string cType = ShowGrantTreeReader2["Type"].ToString().Trim();            //结点类型
            int iHomologID = (int)ShowGrantTreeReader2["Page_ID"];                       //若是页面其对应的页面ID
            string sLabel = ShowGrantTreeReader2["Label"].ToString().Trim();           //结点标签
            string sIFDbGrant = ShowGrantTreeReader2["IFDbGrant"].ToString().Trim();       //是否可进行组织部门选择

            //组合html语句
            string sLabelHtml = "&nbsp;" + sLabel;
            if (sCode.Equals("Men"))
            { //根结点

                string sRootHtml = "";
                if (isLeafed.Equals("0"))
                { //无子结点
                    sRootHtml = sRootLeaflessIconHtml + sFolderCloseIconHtml + sLabelHtml;
                    break;
                }
                else
                    sRootHtml = sRootSpreadIconHtml + sFolderOpenIconHtml + sLabelHtml;
                sTreeHtml = sTreeHtml + "<div>" + sRootHtml + "</div>";
            }
            else
            { //非根结点
                string sNodeHtml = "";
                // 处理缩进 
                string sIndentHtml = "";
                int i = 0;
                char cIsVertical;
                while (i < sIndentVertical.Length)
                {
                    cIsVertical = sIndentVertical[i];
                    if (cIsVertical == '0')
                        sIndentHtml = sIndentHtml + sBlankIconHtml;
                    else
                        sIndentHtml = sIndentHtml + sVerticalIconHtml;
                    i = i + 1;
                }

                // 结点 
                if (cType.Equals("1"))
                { //1.树枝结点
                    if (isLastSibling.Equals("0"))
                    { //2.非尾结点
                        if (isLeafed.Equals("1")) //3.有子结点
                            sNodeHtml = sMidSpreadIconHtml + sFolderOpenIconHtml + sLabelHtml;
                        else //3.无子结点
                            sNodeHtml = sMidLeaflessIconHtml + sFolderCloseIconHtml + sLabelHtml;
                    }
                    else
                    { //2.尾结点

                        if (isLeafed.Equals("1")) //3.有子结点
                            sNodeHtml = sRearSpreadIconHtml + sFolderOpenIconHtml + sLabelHtml;
                        else //3.无子结点
                            sNodeHtml = sRearLeaflessIconHtml + sFolderCloseIconHtml + sLabelHtml;
                    }
                }
                else if (cType.Equals("0"))
                { //1.树叶结点
                    nPageCount++;
                    //BtnInfo = UMConn.MyBtnInfo(sUserRoleCode, iHomologID);

                    //string sPageChkboxHtml = "<input type=\"checkbox\" name=\"chkPage\" value=\"" + iHomologID + "\" onclick=\"pageChkboxClick(this)\"/>&nbsp;&nbsp;" + BtnInfo;
                    string sOrgPersChoice = "";
                    string sRoleChoice = "";
                    string sDocChoice = "";
                    if (sIFDbGrant.Trim() == "1")   //可以进行数据角色授权
                    {
                        if (iHomologID == 300 || iHomologID == 301 || iHomologID == 302 || iHomologID == 303 || iHomologID == 304)
                        {
                            //文件和文件夹选择
                            //sDocChoice = "&nbsp;<input type=\"text\" size=\"20\" id=\"ChoiceDocName_" + iHomologID.ToString().Trim() + "\" onclick=\"GetMyDocTreeClick(" + iHomologID.ToString().Trim() + ")\" runat=\"server\" />";
                           // sDocChoice = sDocChoice + " <input type=\"hidden\"  size=\"20\" id=\"HChoiceDocID_" + iHomologID.ToString().Trim() + "\" runat=\"server\" /> ";
                           // sDocChoice = sDocChoice + "<input id=\"GrantBtn1\" type=\"button\"  onclick=\"GetMyDocTreeClick(" + iHomologID.ToString().Trim() + ")\" value =\"文档\" /> ";
                            sDocChoice = "&nbsp;<input type=\"text\" size=\"20\" id=\"ChoiceDocName_" + iHomologID.ToString().Trim() + "\" onclick=\"GetMyFolderFileTree(" + iHomologID.ToString().Trim() + ")\" runat=\"server\" />";
                            sDocChoice = sDocChoice + " <input type=\"hidden\"  size=\"20\" id=\"HChoiceDocID_" + iHomologID.ToString().Trim() + "\" runat=\"server\" /> ";
                            sDocChoice = sDocChoice + "<input id=\"GrantBtn1\" type=\"button\"  onclick=\"GetMyFolderFileTree(" + iHomologID.ToString().Trim() + ")\" value =\"文档\" /> ";

                            sPageDocIDCol = sPageDocIDCol + iHomologID.ToString().Trim() + ",";   //可以数据角色授权的页面ID集合
                        }
                        else
                        {
                            //组织人事选择 
                            sOrgPersChoice = "&nbsp;<input type=\"text\" size=\"20\" id=\"ChoiceName_" + iHomologID.ToString().Trim() + "\" onclick=\"GetMyOrgTreeClick(" + iHomologID.ToString().Trim() + ")\"  runat=\"server\" />";
                            sOrgPersChoice = sOrgPersChoice + " <input type=\"hidden\"  size=\"20\" id=\"HChoiceID_" + iHomologID.ToString().Trim() + "\" runat=\"server\" /> ";
                            sOrgPersChoice = sOrgPersChoice + "<input id=\"GrantBtn1\" type=\"button\"  onclick=\"GetMyOrgTreeClick(" + iHomologID.ToString().Trim() + ")\" value =\"组织\" /> ";

                            //角色选择
                            sRoleChoice = "&nbsp;<input type=\"text\" size=\"20\" id=\"ChoiceRoleName_" + iHomologID.ToString().Trim() + "\" onclick=\"GetMyRoleTree(" + iHomologID.ToString().Trim() + ")\" runat=\"server\" />";
                            sRoleChoice = sRoleChoice + " <input type=\"hidden\"  size=\"20\" id=\"HChoiceRoleID_" + iHomologID.ToString().Trim() + "\" runat=\"server\"/> ";
                            sRoleChoice = sRoleChoice + "<input id=\"GrantBtn1\" type=\"button\"  onclick=\"GetMyRoleTree(" + iHomologID.ToString().Trim() + ")\" value =\"角色\" /> ";

                            sPageIDCol = sPageIDCol + iHomologID.ToString().Trim() + ",";   //可以数据角色授权的页面ID集合
                        }                        
                    }

                    if (isLastSibling.Equals("0")) //2.非尾结点
                        //sNodeHtml = sMidLeaflessIconHtml + sPageUnselectedIconHtml +sLabelHtml + sPageChkboxHtml + sBtnHtml;
                        sNodeHtml = sMidLeaflessIconHtml + sPageUnselectedIconHtml + sLabelHtml + sOrgPersChoice + sRoleChoice + sDocChoice;
                    else //2.尾结点
                        //sNodeHtml = sRearLeaflessIconHtml + sPageUnselectedIconHtml +sLabelHtml + sPageChkboxHtml + sBtnHtml;
                        sNodeHtml = sRearLeaflessIconHtml + sPageUnselectedIconHtml + sLabelHtml + sOrgPersChoice + sRoleChoice + sDocChoice;
                }
                sNodeHtml = sIndentHtml + sNodeHtml;
                sTreeHtml = sTreeHtml + "<div>" + sNodeHtml + "</div>";
            }
        }
        if (ShowGrantTreeReader2 != null)
            ShowGrantTreeReader2.Close();

        sPageIDCol = sPageIDCol + "\"";
        sPageDocIDCol = sPageDocIDCol + "\"";

        //选择组织树
        string sJS10 = "var sOrgIDCol = form1.HOrgIDCol.value;var sFeatures = \"dialogHeight:600px;dialogWidth:700px;resizeable:no;help:no;status:no\"; var obj = showModalDialog(\"ChoiceOrgPersInfo.aspx?ChoiceID=\"+obj+\"&OrgType=" + sOrgtype + "&OrgIDCol=\" + sOrgIDCol,window,sFeatures);if(obj != undefined){ sChoiceID = obj.sChoiceID;ChoiceNameInfo = obj.ChoiceNameInfo;ChoiceIDInfo=obj.ChoiceIDInfo; eval(\"form1.ChoiceName_\"+sChoiceID+\".value=\"+ChoiceNameInfo+\";\"); eval(\"form1.HChoiceID_\"+sChoiceID+\".value=\" + ChoiceIDInfo + \";\"); eval(\"form1.ChoiceName_\"+sChoiceID+\".blur();\"); }";
        string sPageChkboxClickJS = "var sChoiceID;var ChoiceNameInfo;var ChoiceIDInfo;var iChoicePoNumber =0; function GetMyOrgTreeClick(obj){ " + sJS10 + "}";

        //选择角色树
        string sJS11 = "var sFeatures = \"dialogHeight:600px;dialogWidth:400px;resizeable:no;help:no;status:no\"; var obj = showModalDialog(\"ChoiceRoleTree.aspx?PageID=\"+sPageID+\"&OrgType=" + sOrgtype + "\",window,sFeatures);if(obj != undefined){ sPageRoleInfo = obj.sPageRoleInfo;ChoiceRoleNameInfo = obj.ChoiceRoleNameInfo;ChoiceRoleIDInfo=obj.ChoiceRoleIDInfo; eval(\"form1.ChoiceRoleName_\"+sPageRoleInfo+\".value=\"+ChoiceRoleNameInfo+\";\"); eval(\"form1.HChoiceRoleID_\"+sPageRoleInfo+\".value=\"+ChoiceRoleIDInfo+\";\");  eval(\"form1.ChoiceRoleName_\"+sPageRoleInfo+\".blur();\"); } ";
        string sPageRoleClickJS = "var sPageRoleInfo;var ChoiceRoleNameInfo;var ChoiceRoleIDInfo;var iChoiceRolePoNumber =0; function GetMyRoleTree(obj){ var sPageID=obj; " + sJS11 + "}";

        //选择文档树
        string sJS12 = "var sOrgIDCol = form1.HOrgIDCol.value;var sFeatures = \"dialogHeight:600px;dialogWidth:700px;resizeable:no;help:no;status:no\"; var obj = showModalDialog(\"../common/ChoiceFolderFiles_Index.aspx?ChoiceID=\"+obj,window,sFeatures);if(obj != undefined){ sChoiceFolderFileID = obj.sChoiceFolderFileID;ChoiceFolderFileNameInfo = obj.ChoiceFolderFileNameInfo;ChoiceFolderFileIDInfo=obj.ChoiceFolderFileIDInfo; eval(\"form1.ChoiceDocName_\"+sChoiceFolderFileID+\".value=\"+ChoiceFolderFileNameInfo+\";\"); eval(\"form1.HChoiceDocID_\"+sChoiceFolderFileID+\".value=\" + ChoiceFolderFileIDInfo + \";\"); eval(\"form1.ChoiceDocName_\"+sChoiceFolderFileID+\".blur();\"); }";
        string sPageDocClickJS = "var sChoiceFolderFileID;var ChoiceFolderFileNameInfo;var ChoiceFolderFileIDInfo;var iChoiceDocNumber =0; function GetMyFolderFileTree(obj){ " + sJS12 + "}";

        string sJS = "<script>" + sPageChkboxClickJS + sPageRoleClickJS + sPageDocClickJS + "form1.PageIDCol.value=" + sPageIDCol + ";form1.PageDocIDCol.value=" + sPageDocIDCol + ";</script>";
        sHtml = sTreeHtml + sJS;
        ViewState["VHtml"] = sHtml;
    }

    /// <summary>
    /// 保存
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void SaveInfo_Click(object sender, EventArgs e)
    {
        if (Role_Code.Text.Trim() == "")
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('角色编码不能为空!');</script>");
            return;
        }
        if (Role_Name.Text.Trim() == "")
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('角色名称不能为空!');</script>");
            return;
        }
        /*if (Ctrl_Role.SelectedValue.Trim().Trim() == "0")
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择功能操作角色!');</script>");
            return;
        }*/

        string sCreator_DataRole = "";//创建者的数据角色
        try
        {
            sCreator_DataRole = Session["Baimasoft_Role_Name"].ToString().Trim(); //功能操作角色编码ID
        }
        catch
        {
        }

        string s1, s2, s3, s4, s5, s6, s7, s8;
        s1 = Role_Code.Text.Trim();
        string hRoleCode = HRole_Code.Value.ToString();
        s2 = Role_Name.Text.Trim();
        string hRoleName = HRole_Name.Value.ToString().Trim();

        if (hRoleCode != s1)
        {
            int iRoleCode = MyDBRM .JudgeDataRoleCode (s1).Rows .Count ;
            if (iRoleCode == 1)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('角色编码已创建!');</script>");
                return;
            }
        }

        if (hRoleName != s2)
        {
            int iRoleCode = MyDBRM .JudgeDataRoleName (s2).Rows .Count ;
            if (iRoleCode == 1)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('角色名称已创建!');</script>");
                return;
            }
        }

        s3 = Role_Descr.Text.Trim();
        s5 = sCreator_DataRole;
        s6 = Item_IDList.Value.Trim();//数据项链表

        s7 = Field_IDList.Value.Trim();//字段链表
        s8 = Effective.SelectedValue.Trim();
        string sPOPIDCol = PageOrgPersIDCol.Value.ToString().Trim();   //页面组织人员ＩＤ的组合
        string sPOPNameCol = PageOrgPersNameCol.Value.ToString().Trim();   //页面组织人员Name的组合


        string sPageRoleIDCol = PageRoleIDCol.Value.ToString().Trim();     //所授权角色ID
        string sPageRoleNameCol = PageRoleNameCol.Value.ToString().Trim();  //所授权角色名称

        string sPageDocIDCol   = PageFolderFileIDCol .Value.ToString().Trim();   //文档ID集合
        string sPggeDocNameCol = PageFolderFileNameCol.Value.ToString().Trim();  //文档名称集合

        if (s3.Length > 100)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('角色描述不能大于100字!');</script>");
            return;
        }

        if (hidEditType.Value.ToString().Trim() == "1")//添加
        {
            s4 = Ctrl_Role.SelectedValue.Trim();
            if (s4 == "0")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择功能操作角色!');</script>");
                return;
            }

            int back = MyDBRM.InsertDbRoleInfo(s1, s2, s3, s4, s5, sPOPIDCol, sPOPNameCol, sPageRoleIDCol, sPageRoleNameCol, s7, s8, "", sPOPIDCol, Field_IDList.Value.Trim(), sPageDocIDCol, sPggeDocNameCol);
            if (back >= 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加成功!');window.close();</script>");
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加失败!');</script>");
            }
        }
        if (hidEditType.Value.ToString().Trim() == "2")//修改
        {
            s4 = HCtrl_Role.Value.ToString().Trim();
            int back = MyDBRM.UpdateDbRoleInfo(hRoleCode, 2, s1, s2, s3, s4, s5, sPOPIDCol, sPOPNameCol, sPageRoleIDCol, sPageRoleNameCol, s7, s8, "", "", sPOPIDCol, Field_IDList.Value.Trim(), sPageDocIDCol, sPggeDocNameCol);
            if (back >= 1)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功!');window.close();</script>");
                return;
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改失败!');</script>");
                return;
            }
        }
    }
}